/*var path=chooseFile();
log( path.substring(path.lastIndexOf("/")+1) );
*/
toastLog(chooseFile())

function chooseFile(path){
    try{ fileIcon;}catch(e){fileIcon="🤪"}
    try{ dirIcon;}catch(e){dirIcon="📂"}
    if(path==undefined){
        path=files.getSdcardPath();
    }
    var items, fileList, dirList, ch;
    //选择或退出选择文件
    while(files.isDir(path)){
        items=[ "🔙上一级", "❎退出", "*️⃣根目录", "🔎搜索"];
        dirList=[];
        fileList=[];
        //获取子文件与子文件夹
        files.listDir(path, function(name){
            if( files.isFile( files.join( path, name)) ){
                fileList.push(name);
            } else {
                dirList.push(name);
            }
            return false;
        });
        //排序
        dirList=dirList.sort((a, b) => a.localeCompare(b));
        fileList=fileList.sort((a, b) => a.localeCompare(b));
        //给文件名字随机生成一个前缀并保存到数组items中
        fileList.forEach(n=>{
            items.push(fileIcon+n);
            fileIcon=randomIcon();
        });
        dirList.forEach(n=>{
            items.push(dirIcon+n);
        });
        ch=dialogs.select("当前位置："+path, items);
        switch(ch){
            case -1: ch;
            case 0: 
                var len=path.lastIndexOf("/");
                if(len==path.length-1){
                    len=path.lastIndexOf( "/", len-1);
                }
                path=path.substring( 0, len); 
                break;
            case 1: 
                toastLog("退出文件选择");
                return null;
            case 2: 
                path=files.getSdcardPath();
                break;
            case 3:
                path=files.join( path, searchByName( dialogs.input("输入要搜索的关键字，支持多关键字搜索，使用空格分隔"), items, 4)
                                                .substring(2));//搜索时会将前缀特殊符号带进去搜，所以需要截断
                break;
            default: 
                var t;
                if(ch<items.length-dirList.length){
                    t=fileList[ch-(items.length-fileList.length-dirList.length)];
                }else{
                    t=dirList[ch-(items.length-dirList.length)];
                }
                path=files.join( path, t);
        }
    }
    if(files.isFile(path)){
        return path;
    } else {
        toastLog("选择文件错误！"+"\n"+path);
        return null;
    }
    //随机返回一个前缀
    function randomIcon(arr){
        if(arr==undefined){
            arr=["❤️","💙","🤎","🧡","💜","💔","💛","🖤","💚","🤍","💖","❣️","💗","💓","💝","💘","💕","💞","💟"
            ,"☮️","✝️","☪️","🕉","☸️","✡️","🔯","🕎","☯️","☦️","🛐"
            ,"⛎","♈️","♉️","♊️","♌️","♎️","♏️","♐️","♑️","♒️","♓️","⚛️"
            ,"㊙️","🈲","💯","🆘","🇨🇳","🏴","🏳️","🏁"
            ,"🌠","🎇","🎆","🌌","🌉","🌃","💫","✨"
            ,"💩"]
        }
        try{
            if(iconArr.length<1){
                throw "更新图标数组";
            }
        }catch(e){ 
            iconArr=arr; 
        }
        return iconArr[random( 0, iconArr.length-1)];
    }
    //搜索 word关键字，arr字符串数组，startIndex起始下标
    function searchByName( word, arr, startIndex){
        var name="";
        word=word.toString().split(" ");
        for(var i=0; i<word.length; i++){
            if(word[i]==""){
                word[i]=" ";//解决无法搜索空格的问题
            }
        }
        var items=["🔎继续搜索"];
        //匹配，将匹配的名字保存到items中
        word.forEach(w=>{
            for(var i=startIndex; i<arr.length; i++){
                if(arr[i].indexOf(w)>-1){
                    var noExists=true;
                    for(var j=0; j<items.length; j++){
                        if(w==items[j]){
                            noExists=false;
                            break;
                        }
                    }
                    if(noExists){
                        items.push(arr[i]);
                    }
                }
            }
        });
        var ch=dialogs.select( "["+word+"]", items);
        if(ch==0){
            try{
                return searchByName( dialogs.input("["+word+"]\n继续搜索"), items, 1);
            }catch(e){}
        }else if(ch>0){
            name=items[ch];
        }
        return name;
    }
}